//
//  DKFaqQueryCell.m
//  DanKU
//
//  Created by 吴斌 on 2023/11/12.
//

#import "DKFaqQueryCell.h"

static NSString * FaqQueryCellTableId = @"FaqQueryCellTableId";
@interface DKFaqQueryCell()
@property (nonatomic, strong)UILabel * faqSourchQuestionLab;
@property (nonatomic, strong)UILabel * faqSourchAnswerLab;
@property (nonatomic, strong)UIButton * faqSourchDownBtn;
@property (nonatomic, strong)UIView * faqSourchResolvedLineView;

@end
@implementation DKFaqQueryCell
+ (instancetype)faqQueryCell:(UITableView *)tableView{
    DKFaqQueryCell * faqQueryCell = [tableView dequeueReusableCellWithIdentifier:FaqQueryCellTableId];
    if (!faqQueryCell) {
        faqQueryCell = [[DKFaqQueryCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:FaqQueryCellTableId];
    }
    faqQueryCell.backgroundColor = [UIColor whiteColor];
    faqQueryCell.selectionStyle = UITableViewCellSelectionStyleNone;
    faqQueryCell.accessoryType = UITableViewCellAccessoryNone;
    return faqQueryCell;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self creatSubView];
    }
    return self;
}
- (void)creatSubView{
    self.faqSourchQuestionLab = [[UILabel alloc] init];
    self.faqSourchQuestionLab.textColor = DK_Hex(@"#333333");
    self.faqSourchQuestionLab.font = DK_FONT_Medium(15);
    self.faqSourchQuestionLab.numberOfLines = 0;
    [self.contentView addSubview:self.faqSourchQuestionLab];
    [self.faqSourchQuestionLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@(DK_IsWidthScale(20)));
        make.left.equalTo(@(DK_IsWidthScale(15)));
        make.right.equalTo(@(-DK_IsWidthScale(55)));
        make.height.equalTo(@(DK_IsWidthScale(0)));
    }];
    self.faqSourchDownBtn = [[UIButton alloc] init];
    [self.faqSourchDownBtn setBackgroundImage:DK_GetImage(@"dk_MeFaq_Ping") forState:UIControlStateNormal];
    [self.faqSourchDownBtn setBackgroundImage:DK_GetImage(@"dk_MeFaq_Down") forState:UIControlStateSelected];
    [self.faqSourchDownBtn addTarget:self action:@selector(faqSourchDown) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.faqSourchDownBtn];
    [self.faqSourchDownBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@(DK_IsWidthScale(20)));
        make.right.equalTo(@(-DK_IsWidthScale(15)));
        make.width.equalTo(@(DK_IsWidthScale(20)));
        make.height.equalTo(@(DK_IsWidthScale(20)));
    }];
    self.faqSourchAnswerLab = [[UILabel alloc] init];
    self.faqSourchAnswerLab.textColor = DK_Hex(@"#999999");
    self.faqSourchAnswerLab.font = DK_FONT_Regular(12);
    self.faqSourchAnswerLab.numberOfLines = 0;
    [self.contentView addSubview:self.faqSourchAnswerLab];
    [self.faqSourchAnswerLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.faqSourchQuestionLab.mas_bottom).offset(DK_IsWidthScale(10));
        make.left.equalTo(@(DK_IsWidthScale(15)));
        make.right.equalTo(@(-DK_IsWidthScale(55)));
        make.height.equalTo(@(DK_IsWidthScale(0)));
        make.bottom.equalTo(@(-DK_IsWidthScale(20)));
    }];
    self.faqSourchResolvedLineView = [[UIView alloc] init];
    self.faqSourchResolvedLineView.backgroundColor = DK_Hex(@"#E0E0E0");
    [self.contentView addSubview:self.faqSourchResolvedLineView];
    [self.faqSourchResolvedLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(15)));
        make.right.equalTo(@(-DK_IsWidthScale(15)));
        make.height.equalTo(@(DK_IsWidthScale(0.5)));
        make.bottom.equalTo(@(DK_IsWidthScale(0)));
    }];
}
-(void)setFaqTypeMdl:(DKFaqTypeModel *)faqTypeMdl{
    _faqTypeMdl = faqTypeMdl;
    if(_faqTypeMdl){
        NSMutableAttributedString * faqSourchtAnswerAttritedString = [[[NSAttributedString alloc]initWithData:[_faqTypeMdl.answer dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType} documentAttributes:nil error:nil] mutableCopy];
        [faqSourchtAnswerAttritedString addAttributes:@{NSFontAttributeName:DK_FONT_Regular(12)} range:NSMakeRange(0, faqSourchtAnswerAttritedString.length)];
        if(_faqTypeMdl.isDown){
//            CGFloat faqSourchtAnswerH = ceilf([NSString getContentSize:_faqTypeMdl.answer withFont:DK_FONT_Regular(12) withSize:CGSizeMake(DK_SCREEN_W - DK_IsWidthScale(60), CGFLOAT_MAX)].height);
            
            CGFloat faqSourchtAnswerH = ceilf([NSString getContentAttributedSize:faqSourchtAnswerAttritedString withSize:CGSizeMake(DK_SCREEN_W - DK_IsWidthScale(60), CGFLOAT_MAX)].height+1);
            [self.faqSourchAnswerLab mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.equalTo(@(faqSourchtAnswerH));
                make.top.equalTo(self.faqSourchQuestionLab.mas_bottom).offset(DK_IsWidthScale(10));
            }];
        }else{
            [self.faqSourchAnswerLab mas_updateConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.faqSourchQuestionLab.mas_bottom).offset(DK_IsWidthScale(0));
                make.height.equalTo(@(DK_IsWidthScale(0)));
            }];
        }
//        CGFloat faqSourchtQuestionH = ceilf([NSString getContentSize:_faqTypeMdl.question withFont:DK_FONT_Medium(15) withSize:CGSizeMake(DK_SCREEN_W - DK_IsWidthScale(100), CGFLOAT_MAX)].height);
        NSMutableAttributedString * faqSourchtQuestionAttritedString = [[[NSAttributedString alloc]initWithData:[_faqTypeMdl.question dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType} documentAttributes:nil error:nil] mutableCopy];
        [faqSourchtQuestionAttritedString addAttributes:@{NSFontAttributeName:DK_FONT_Medium(15)} range:NSMakeRange(0, faqSourchtQuestionAttritedString.length)];
        CGFloat faqSourchtQuestionH = ceilf([NSString getContentAttributedSize:faqSourchtQuestionAttritedString withSize:CGSizeMake(DK_SCREEN_W - DK_IsWidthScale(100), CGFLOAT_MAX)].height+1);
        
        [self.faqSourchQuestionLab mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@(faqSourchtQuestionH));
        }];
        _faqSourchQuestionLab.attributedText = faqSourchtQuestionAttritedString;
        _faqSourchAnswerLab.attributedText = faqSourchtAnswerAttritedString;
        _faqSourchDownBtn.selected = _faqTypeMdl.isDown;
    }
}
- (void)faqSourchDown{
    if(self.UpdataTableBlock){
        self.UpdataTableBlock(self.indexRow);
    }
}
@end
